//
//  ZTLoopView.m
//  WisdomCommunity
//
//  Created by Yahaong on 16/7/26.
//  Copyright © 2016年 zonten. All rights reserved.
//

#import "ZTLoopView.h"
#import "AddressView.h"
@interface ZTLoopView ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView            *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout  *flowLayout;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) AddressView *addressView;

@property (nonatomic, assign) CGFloat oldOffset;

@end
@implementation ZTLoopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initial];
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
    self.flowLayout.itemSize = self.bounds.size;
    
    CGFloat pageW  = 80;
    CGFloat pageH  = 30;
    
    self.pageControl.yh_width = pageW;
    self.pageControl.yh_height = pageH;
    self.pageControl.yh_centerX = self.yh_centerX;

    self.pageControl.yh_y = self.yh_height - pageH;
    self.addressView.yh_width = 180;
    self.addressView.yh_height = 30;
    self.addressView.yh_centerX = kScreenWidth * 0.5;

    self.addressView.yh_y = self.yh_y + 12;

}
- (void)initial
{
    [self addSubview:self.collectionView];
   [self addSubview:self.addressView];
    [self addSubview:self.pageControl];
   
}
- (void)setImageArray:(NSArray *)imageArray
{
    _imageArray = [imageArray copy];
    if (self.timer != nil) {
        [self stopTimer];
    }
    if (self.imageArray.count > 0) {
        self.pageControl.numberOfPages = imageArray.count;
        [self.collectionView reloadData];
        [self startTimer];
    }
}

#pragma mark <UICollectionViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.oldOffset > self.collectionView.contentOffset.x)
    {
        if (self.collectionView.contentOffset.x < 0)
        {
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.imageArray.count inSection:0]atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
        }
    }
    else {
        if (self.collectionView.contentOffset.x > (self.collectionView.contentSize.width - self.frame.size.width) )
        {
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0 ]atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
        }
    }
    self.pageControl.currentPage = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
    
    self.oldOffset = self.collectionView.contentOffset.x;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    if (self.collectionView.contentOffset.x > (self.imageArray.count - 1) *  self.frame.size.width) {
        self.pageControl.currentPage = 0;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.pageControl.currentPage = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
    [self startTimer];
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.item == self.imageArray.count) {
        return;
    }
    
}

- (void)startTimer
{
    [self stopTimer];
    if (self.imageArray.count == 1)
    {
        return;
    }
    self.timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(next) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)next
{
    CGFloat newOffset = self.collectionView.contentOffset.x + self.frame.size.width;
    if (newOffset == (self.collectionView.contentSize.width - self.frame.size.width)) {
        newOffset += 1;
    }
    CGPoint offset = CGPointMake(newOffset, 0);
    [self.collectionView setContentOffset:offset animated:YES];
}

- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.imageArray.count == 1) {
        return 1;
    }else
    {
        return self.imageArray.count + 1;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    UIImage *  image = 0;
    if (indexPath.row == self.imageArray.count ){
        image =  [UIImage imageNamed: self.imageArray.firstObject];
    }else {
        image =  [UIImage imageNamed: self.imageArray[indexPath.row]];
    }
    cell.layer.contents = (id)image.CGImage;
    return cell;
}

#pragma mark <懒加载>

- (UICollectionViewFlowLayout *)flowLayout
{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _flowLayout;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    }
    return _collectionView;
}
-(UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.hidesForSinglePage = YES;
        _pageControl.currentPage = 0;
        _pageControl.numberOfPages = self.imageArray.count;
        _pageControl.backgroundColor = [UIColor clearColor];
        _pageControl.pageIndicatorTintColor = RGBColor(153, 153, 153);//圆点颜色
       _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];//当前圆点颜色

        
    }
    return _pageControl;
}



- (NSTimeInterval)interval
{
    return _interval ? 3 :_interval;
}
- (CGFloat)currentOffset
{
    return self.collectionView.contentOffset.x;
}

- (AddressView *)addressView
{
    if (!_addressView) {
        _addressView = [[AddressView alloc]init];
       
    }
    return _addressView;
}
@end
